﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal class OrderItemPricingDAO : MySQLDAO, IOrderItemPricingDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<OrderItemPricingDTO> cmdExec;

        #endregion

        #region Contructor

        public OrderItemPricingDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<OrderItemPricingDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO order_item_pricing_tbl (amount, currency_code) " +
            "VALUES (@amount, @currency_code)";
        public void Insert(OrderItemPricingDTO dto)
        {
            cmdExec.PerformNonQuery(insertSQL, loadParametersFromDTO(dto));
            dto.ID = cmdExec.GetLastID();
        }

        private const string selectByIDSQL =
            "SELECT * FROM order_item_pricing_tbl WHERE order_item_pricing_id = @order_item_pricing_id";
        public OrderItemPricingDTO SelectByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_item_pricing_id"] = id;

            MySQLDAOCmdExecuter<OrderItemPricingDTO>.LoadDTODelegate loadOrderItemPricingDTO =
                new MySQLDAOCmdExecuter<OrderItemPricingDTO>.LoadDTODelegate(loadDTOFromReader);
            OrderItemPricingDTO dto =
                (OrderItemPricingDTO)cmdExec.PerformSingletonSelectQuery(selectByIDSQL, parameters, loadOrderItemPricingDTO);

            return dto;
        }

        const string selectAllSQL =
            "SELECT * FROM order_item_pricing_tbl";
        public IList<OrderItemPricingDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<OrderItemPricingDTO>.LoadDTODelegate loadOrderItemPricingDTO =
                new MySQLDAOCmdExecuter<OrderItemPricingDTO>.LoadDTODelegate(loadDTOFromReader);
            return (IList<OrderItemPricingDTO>)cmdExec.PerformSelectQuery(selectAllSQL, null, loadOrderItemPricingDTO);
        }

        private const string updateSQL =
            "UPDATE order_item_pricing_tbl SET amount = @amount, currency_code = @currency_code, " +
            "WHERE order_item_pricing_id = @order_item_pricing_id";
        public void Update(OrderItemPricingDTO dto)
        {
            cmdExec.PerformNonQuery(updateSQL, loadParametersFromDTO(dto));
        }

        const string deleteByIDSQL =
            "DELETE FROM order_item_pricing_tbl WHERE order_item_pricing_id = @order_item_pricing_id";
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_item_pricing_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(OrderItemPricingDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_item_pricing_id"] = dto.ID;
            parameters["@amount"] = dto.Amount;
            parameters["@currency_code"] = dto.CurrencyCode;
            return parameters;
        }

        private OrderItemPricingDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            OrderItemPricingDTO dto = new OrderItemPricingDTO();
            dto.ID = dataReader.GetInt64("order_item_pricing_id");
            dto.Amount = dataReader.GetDecimal("amount");
            dto.CurrencyCode = dataReader.GetString("currency_code");
            return dto;
        }

        #endregion
    }
}
